//package com.yifushidai.service;
//
//import com.yifushidai.entity.BandLockEntity;
//import com.yifushidai.entity.IdcardBindEntity;
//import com.yifushidai.entity.IdcardEntity;
//import com.yifushidai.entity.OptDeviceLog;
//import com.yifushidai.mapper.BandLockEntityMapper;
//import com.yifushidai.mapper.IdcardBindEntityMapper;
//import com.yifushidai.mapper.IdcardEntityMapper;
//import com.yifushidai.mapper.OptDeviceLogMapper;
//import com.yifushidai.utils.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.servlet.http.HttpServletRequest;
//import java.text.ParseException;
//import java.util.Date;
//import java.util.LinkedHashMap;
//import java.util.List;
//import java.util.Map;
//
//@Service
//public class IdcardService {
//    @Autowired
//    private BandLockEntityMapper lockMapper;
//    @Autowired
//    private IdcardEntityMapper idcardEntityMapper;
//    @Autowired
//    private IdcardBindEntityMapper idcardBindEntityMapper;
//    @Autowired
//    private OptDeviceLogMapper optDeviceLogMapper;
//
//    public R load(String mac, HttpServletRequest request) throws InterruptedException {
//        BandLockEntity bandlock = lockMapper.queryByMac(mac);
//        if (bandlock == null) {
//            return R.error(ErrorCodeEnum.DATA_NULL);
//        }
//        if (bandlock.getLs() == 0) {//掉线
//            return R.error(ErrorCodeEnum.ONLINE_ERROR);
//        }
//
//        String prefix = "录入身份证";
//        Object res = null;
//        int count = 0;
//        while (res == null && count < 5) {
//            count++;
//            res = ApartmentMinaPushUtils.push(mac, CustomPackUtils.IDCARD_LOAD_HEX_CMD, CustomPackUtils.IDCARD_READER_DEVICE_ID, CustomPackUtils.IDCARD_LOAD_MSG_ID);
//        }
//        if (res instanceof IdcardEntity) {
//            optDeviceLogMapper.insertSelective(new OptDeviceLog(IpUtil.getIpAddress(request), bandlock.getMac(), null, bandlock.getAddr() + bandlock.getAlias(), prefix + " 任务成功"));//操作日志
//            return R.ok().put(res);
//        } else {
//            optDeviceLogMapper.insertSelective(new OptDeviceLog(IpUtil.getIpAddress(request), bandlock.getMac(), null, bandlock.getAddr() + bandlock.getAlias(), prefix + " 任务失败"));//操作日志
//            return R.error(ErrorCodeEnum.MINA_PUSH_ERROR);
//        }
//    }
//
//    public R selectByGmsfzhm(String gmsfzhm) {
//        IdcardEntity idcardEntity = idcardEntityMapper.selectByGmsfzhm(gmsfzhm);
//        return R.ok().put(idcardEntity);
//    }
//
//    public R bind(String mac, String idcardType, String idcardUid, String expireTime, HttpServletRequest request) throws InterruptedException, ParseException {
//        Date expireDate = DateUtils.toDate(expireTime);
//        if (expireDate.getTime() < new Date().getTime()) {
//            return R.error(ErrorCodeEnum.ARGUMENT_VALIDATION);
//        }
//        if (!idcardType.matches("^D\\d$") || !idcardUid.matches("^\\w{16}$")) {
//            return R.error(ErrorCodeEnum.ARGUMENT_VALIDATION);
//        }
//        BandLockEntity bandlock = lockMapper.queryByMac(mac);
//        if (bandlock == null) {
//            return R.error(ErrorCodeEnum.DATA_NULL);
//        }
//        if (bandlock.getLs() == 0) {//掉线
//            return R.error(ErrorCodeEnum.ONLINE_ERROR);
//        }
//
//        IdcardEntity idcardEntity = idcardEntityMapper.selectByUid(idcardUid);
//        if (idcardEntity == null) {
//            return R.error(ErrorCodeEnum.DATA_NULL);
//        }
//        Map<String, Object> params = new LinkedHashMap<>();
//        params.put("mac", mac);
//        params.put("idcardType", idcardType);
//        params.put("idcardUid", idcardUid);
//        IdcardBindEntity db1 = idcardBindEntityMapper.selectByMacAndIdcardType(params);
//        if (db1 != null) {
//            return R.error(ErrorCodeEnum.ICCARD_TYPE_DUPLICATE);
//        }
//        IdcardBindEntity db2 = idcardBindEntityMapper.selectByMacAndIdcardUid(params);
//        if (db2 != null) {
//            return R.error(ErrorCodeEnum.DATA_DUPLICATE);
//        }
//        List<IdcardBindEntity> list = idcardBindEntityMapper.queryByMac(mac);
//        if (list.size() >= 10) {
//            return R.error(ErrorCodeEnum.ICCARD_MAX_BIND);
//        }
//
//        String prefix = "绑定身份证";
//        Object res = ApartmentMinaPushUtils.push(mac, idcardType + idcardUid, CustomPackUtils.UNIQUE_MAC_DEVICE_ID, CustomPackUtils.IDCARD_BIND_MSG_ID);
//        if (CustomPackUtils.RESPONSE_SUCCESS_HEX_CMD.equals(res)) {
//            optDeviceLogMapper.insertSelective(new OptDeviceLog(IpUtil.getIpAddress(request), bandlock.getMac(), null, bandlock.getAddr() + bandlock.getAlias(), prefix + " 任务成功"));//操作日志
//            IdcardBindEntity idcardBindEntity = new IdcardBindEntity();
//            idcardBindEntity.setMac(mac);
//            idcardBindEntity.setIdcardType(idcardType);
//            idcardBindEntity.setIdcardUid(idcardUid);
//            idcardBindEntity.setExpireTime(expireDate);
//            if (idcardBindEntityMapper.insertSelective(idcardBindEntity) > 0) {
//                return R.ok().put(idcardBindEntity);
//            } else {
//                return R.error(ErrorCodeEnum.DATASOURCE_ERROR);
//            }
//        } else {
//            optDeviceLogMapper.insertSelective(new OptDeviceLog(IpUtil.getIpAddress(request), bandlock.getMac(), null, bandlock.getAddr() + bandlock.getAlias(), prefix + " 任务失败"));//操作日志
//            return R.error(ErrorCodeEnum.MINA_PUSH_ERROR);
//        }
//    }
//
//    public R unbind(String mac, String idcardType, HttpServletRequest request) throws InterruptedException {
//        if (!idcardType.matches("^D\\d$")) {
//            return R.error(ErrorCodeEnum.ARGUMENT_VALIDATION);
//        }
//        BandLockEntity bandlock = lockMapper.queryByMac(mac);
//        if (bandlock == null) {
//            return R.error(ErrorCodeEnum.DATA_NULL);
//        }
//        if (bandlock.getLs() == 0) {//门锁掉线时，异常处理
//            return R.error(ErrorCodeEnum.ONLINE_ERROR);
//        }
//
//        String prefix = "解绑身份证";
//        Object res = ApartmentMinaPushUtils.push(mac, idcardType, CustomPackUtils.UNIQUE_MAC_DEVICE_ID, CustomPackUtils.IDCARD_UNBIND_MSG_ID);
//        if (CustomPackUtils.RESPONSE_SUCCESS_HEX_CMD.equals(res)) {
//            optDeviceLogMapper.insertSelective(new OptDeviceLog(IpUtil.getIpAddress(request), bandlock.getMac(), null, bandlock.getAddr() + bandlock.getAlias(), prefix + " 任务成功"));//操作日志
//            Map<String, Object> params = new LinkedHashMap<>();
//            params.put("mac", mac);
//            params.put("idcardType", idcardType);
//            if (idcardBindEntityMapper.deleteByMacAndIdcardType(params) > 0) {
//                return R.ok();
//            } else {
//                return R.error(ErrorCodeEnum.DATASOURCE_ERROR);
//            }
//        } else {
//            optDeviceLogMapper.insertSelective(new OptDeviceLog(IpUtil.getIpAddress(request), bandlock.getMac(), null, bandlock.getAddr() + bandlock.getAlias(), prefix + " 任务失败"));//操作日志
//            return R.error(ErrorCodeEnum.MINA_PUSH_ERROR);
//        }
//    }
//
//    public R roomlogout(String mac, HttpServletRequest request) throws InterruptedException {
//        BandLockEntity bandlock = lockMapper.queryByMac(mac);
//        if (bandlock == null) {
//            return R.error(ErrorCodeEnum.DATA_NULL);
//        }
//        if (bandlock.getLs() == 0) {//门锁掉线时，异常处理
//            return R.error(ErrorCodeEnum.ONLINE_ERROR);
//        }
//
//        String prefix = "房间注销";
//        Object res = ApartmentMinaPushUtils.push(mac, CustomPackUtils.ROOM_LOGOUT_HEX_CMD, CustomPackUtils.UNIQUE_MAC_DEVICE_ID, CustomPackUtils.ROOM_LOGOUT_MSG_ID);
//        if (CustomPackUtils.RESPONSE_SUCCESS_HEX_CMD.equals(res)) {
//            optDeviceLogMapper.insertSelective(new OptDeviceLog(IpUtil.getIpAddress(request), bandlock.getMac(), null, bandlock.getAddr() + bandlock.getAlias(), prefix + " 任务成功"));//操作日志
//            return R.ok().put(idcardBindEntityMapper.deleteByRoomlogout(mac));
//        } else {
//            optDeviceLogMapper.insertSelective(new OptDeviceLog(IpUtil.getIpAddress(request), bandlock.getMac(), null, bandlock.getAddr() + bandlock.getAlias(), prefix + " 任务失败"));//操作日志
//            return R.error(ErrorCodeEnum.MINA_PUSH_ERROR);
//        }
//    }
//
//    public R query(String mac) {
//        return R.ok().put(idcardBindEntityMapper.queryByMac(mac));
//    }
//
////    public R deleteByTime(Date time) {
////        return R.ok().put(idcardBindEntityMapper.deleteByTime(time));
////    }
//}
